package com.ankebio.mybatisgenerator.config;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;

/**
 * 自定义注释生成器
 */
public class MyCommentGenerator extends DefaultCommentGenerator {
//	private Properties properties;
//	private Properties systemPro;
	private boolean suppressDate;
	private boolean suppressAllComments;
//	private String currentDateStr;

	public MyCommentGenerator() {
//		super();
//		properties = new Properties();
//		systemPro = System.getProperties();
//		suppressDate = false;
//		suppressAllComments = false;
//		currentDateStr = (new SimpleDateFormat("yyyy-MM-dd")).format(new Date());
	}

	/**
	 * 对类的注解
	 * @param topLevelClass
	 * @param introspectedTable
	 */
	@Override
	public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		topLevelClass.addJavaDocLine("/**");
		topLevelClass.addJavaDocLine(" * 这是MyBatis Generator自动生成的Model Class.");

		StringBuilder sb = new StringBuilder();
		sb.append(" * 对应的数据表是 : ");
		sb.append(introspectedTable.getFullyQualifiedTable());
		topLevelClass.addJavaDocLine(sb.toString());

		String tableRemarks = introspectedTable.getRemarks();
		if (tableRemarks!=null && !tableRemarks.isEmpty() ) {
			sb.setLength(0);
			sb.append(" * 数据表注释 : ");
			sb.append(tableRemarks);
			topLevelClass.addJavaDocLine(sb.toString());
		}

//		sb.setLength(0);
//		sb.append(" * @author ");
//		sb.append(systemPro.getProperty("user.name"));
//		topLevelClass.addJavaDocLine(sb.toString());

//		String curDateString = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
//		sb.setLength(0);
//		sb.append(" * @date ");
//		sb.append(curDateString);
//		topLevelClass.addJavaDocLine(sb.toString());

		topLevelClass.addJavaDocLine(" */");
	}

	/**
	 * 生成的实体增加字段的中文注释
	 */
	public void addFieldComment(Field field, IntrospectedTable introspectedTable,
	                            IntrospectedColumn introspectedColumn) {
		StringBuilder sb = new StringBuilder();
		field.addJavaDocLine("/**");
		sb.append(" * ");
		sb.append(introspectedColumn.getRemarks());
		field.addJavaDocLine(sb.toString().replace("\n", " "));
		field.addJavaDocLine(" */");
	}



}
